import 'package:efood_multivendor/controller/auth_controller.dart';
import 'package:efood_multivendor/controller/user_controller.dart';
import 'package:efood_multivendor/helper/size_config.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/images.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_app_bar.dart';
import 'package:efood_multivendor/view/base/custom_button.dart';
import 'package:efood_multivendor/view/base/dialog_widget.dart';
import 'package:efood_multivendor/view/base/not_logged_in_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:clipboard/clipboard.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ReferralScreen extends StatefulWidget {
  final String? code;

  const ReferralScreen({Key? key, this.code}) : super(key: key);

  @override
  State<ReferralScreen> createState() => _ReferralScreenState();
}

class _ReferralScreenState extends State<ReferralScreen> {
  bool _isCopied = false;
  bool _isLoggedIn = Get.find<AuthController>().isLoggedIn();

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      if (_isLoggedIn) {
        customAlertDialog();
      }
    });
    super.initState();
  }

  Future<void> customAlertDialog() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if (prefs.getBool(
            'newToReferral${Get.find<UserController>().userInfoModel?.phone}') ==
        null) {
      Get.dialog(
          DialogWidget(
              context: context,
              prefs: prefs,
              title: 'referral_dialog_title'.tr,
              image: 'assets/image/referral.png',
              description: 'referral_dialog_description'.tr,
              fromPage: 'referral'),
          barrierDismissible: false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return _isLoggedIn
        ? Scaffold(
            backgroundColor: Colors.white,
            appBar: CustomAppBar(title: 'referral'.tr),
            body: Container(
              padding: const EdgeInsets.all(Dimensions.PADDING_SIZE_LARGE),
              width: Dimensions.WEB_MAX_WIDTH,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  SingleChildScrollView(
                    physics: BouncingScrollPhysics(),
                    child: Center(
                      child: Column(
                        children: [
                          SizedBox(
                              height: Dimensions.PADDING_SIZE_LARGE * 0.07.h),
                          Center(
                            child: Image.asset(Images.gift_box, height: 270),
                          ),
                          SizedBox(
                            height: 35,
                          ),
                          Padding(
                            padding:
                                const EdgeInsets.symmetric(horizontal: 20.0),
                            child: Text(
                              'invite'.tr,
                              textAlign: TextAlign.center,
                              style: sfMedium.copyWith(
                                  fontSize: Dimensions.fontSizeSmall * 0.44.w,
                                  fontWeight: FontWeight.bold),
                            ),
                          ),
                          SizedBox(
                            height: 3.h,
                          ),
                          Padding(
                            padding:
                                const EdgeInsets.symmetric(horizontal: 20.0),
                            child: Text(
                              'share_code'.tr,
                              textAlign: TextAlign.center,
                              style: sfMedium.copyWith(
                                fontWeight: FontWeight.normal,
                                color: Colors.grey,
                                fontSize: Dimensions.fontSizeSmall * 0.35.w,
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Divider(),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                'copy_code'.tr,
                                style: sfMedium.copyWith(
                                    fontSize: Dimensions.fontSizeSmall * 0.33.w,
                                    fontWeight: FontWeight.bold),
                              ),
                              SizedBox(
                                  height:
                                      Dimensions.PADDING_SIZE_SMALL * 0.2.w),
                              Text(
                                widget.code ?? '',
                                style: sfMedium.copyWith(
                                    fontSize: Dimensions.fontSizeSmall * 0.33.w,
                                    color: Color.fromARGB(255, 155, 155, 155)),
                              ),
                            ],
                          ),
                          IconButton(
                              onPressed: () {
                                FlutterClipboard.copy(widget.code ?? '')
                                    .then((value) {
                                  ScaffoldMessenger.of(context)
                                      .showSnackBar(SnackBar(
                                          backgroundColor: Colors.green,
                                          content: Text(
                                            "copied_to_clipboard".tr,
                                            textAlign: TextAlign.center,
                                          )));
                                });
                                setState(() {
                                  _isCopied = true;
                                });
                              },
                              icon: SvgPicture.asset(
                                'assets/icons/copy.svg',
                                color: _isCopied
                                    ? Colors.grey
                                    : Theme.of(context).colorScheme.secondary,
                              ))
                        ],
                      ),
                      SizedBox(height: Dimensions.PADDING_SIZE_SMALL),
                      Divider(),
                      Container(
                          width: Dimensions.WEB_MAX_WIDTH,
                          padding: EdgeInsets.symmetric(
                              vertical: Dimensions.PADDING_SIZE_SMALL),
                          child: CustomButton(
                            buttonText: 'invite_friend'.tr,
                            onPressed: () async {
                              Share.share(
                                "invite_msg".tr +
                                    " ${widget.code}" +
                                    "download_link".tr,
                              );
                            },
                          )),
                    ],
                  )
                ],
              ),
            ))
        : Scaffold(body: NotLoggedInScreen());
  }
}
